package org.example.groupwarehouse.service;

import org.apache.ibatis.annotations.Param;
import org.example.groupwarehouse.entity.Goods;
import org.example.groupwarehouse.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @author zwy
 * @date 2025/5/14上午12:19
 * @className GoodsService
 * @se www.baidu.com
 * @verdion 1.0
 */
@Service
public class GoodsService {
    //调用mapper方法，拿到所有的商品数据
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MinioService minioService;
    @Autowired
    private CategoryService categoryService;


    public long count() {
        return goodsMapper.count();
    }
    public long countcategory(Integer categoryId, String goodsname) {
        return goodsMapper.countcategory(categoryId,goodsname);
    }
    //返回所有最热的商品数据    
    public List<Goods> selectAllHot() {
        return goodsMapper.selectAllHot();
    }
    public Goods findById(int id) {
        return  goodsMapper.findById(id);
    }
    public List<Goods> findByPage(int page, int pageSize) {
        //先查看是否有对应的缓存数据，如果有，则直接使用。如果没有，则数据库返回数据后加入到缓存中
        List<Goods> list = redisService.getCacheGoodsPage(page);
        if(list == null){
            System.out.println("构建第"+page+"页商品数据缓存");
            //没有缓存
            int offset = (page - 1) * pageSize;//计算数据的偏移量
            list = goodsMapper.selectByPageNew(offset, pageSize);
            //加入到缓存中
            redisService.cacheGoodsPage(page,list);
        }else{
            System.out.println("使用第"+page+"页商品数据缓存");
        }
        return list;
    }
    public List<Goods> findByPage2(int page, int pageSize,Integer categoryId,String goodsname) {
            int offset = (page - 1) * pageSize;//计算数据的偏移量
        return goodsMapper.selectByPagecategory(offset, pageSize, categoryId,goodsname);
    }

    //获取商品照片
    public String uplaodGoods(MultipartFile file) throws Exception {
        //调用minio的方法来保存文件
        return   minioService.uploadFile(file);
    }


    public boolean insertGoods(
            String name,
            String no,
            Integer categoryId,
            Integer stock,
            BigDecimal marketPrice,
            BigDecimal salePrice,
            String image,
            String description,
            String hottest,
            String newest
    ) throws Exception {
        // 1. 检查商品是否已存在

        // 2. 创建新商品
        Goods goods = new Goods();
        goods.setName(name);
        goods.setNo(no);
        goods.setCategoryId(categoryId);
        goods.setStock(stock);
        goods.setMarketPrice(marketPrice);
        goods.setSalePrice(salePrice);
        goods.setImage(image);
        goods.setDescription(description);
        goods.setHottest(hottest);
        goods.setNewest(newest);
        goods.setStatus("0");
        // 通过 atZone(ZoneId.systemDefault()) 设置时区，使用 toInstant() 转换为 Instant 最后Date.from() 转换为 Date 类型
        goods.setSaleTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

        // 3. 保存商品
        int result = goodsMapper.insertGoods(goods);
        return result > 0;
    }

    // 分页查询商品并包含类别名称
    public List<Goods> findByPageWithCategory(
            int page, int pageSize,
            String name, Integer categoryId,
            BigDecimal minPrice, BigDecimal maxPrice
    ) {
        int offset = (page - 1) * pageSize;
        List<Goods> goodsList = goodsMapper.selectByPageWithConditions(
                offset, pageSize, name, categoryId, minPrice, maxPrice
        );

        // 设置分类名称
        for (Goods goods : goodsList) {
            if (goods.getCategoryId() != null) {
                String categoryName = categoryService.getCategoryNameById(goods.getCategoryId());
                goods.setCategoryName(categoryName);
            }
        }
        return goodsList;
    }
    public long countByConditions(
            String name, Integer categoryId,
            BigDecimal minPrice, BigDecimal maxPrice
    ) {
        return goodsMapper.countByConditions(name, categoryId, minPrice, maxPrice);
    }
    //状态更新并清除缓存
    public void updateGoodsStatus(Integer id, String status) {
        goodsMapper.updateGoodsStatus(id, status);
        redisService.deleteAllGoodsPageCache(); // 清除所有分页缓存
    }

}
